package com.haizhi.workstation.mq;

import com.haizhi.workstation.service.PublicSecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.ObjectMessage;

@Component
public class MQReceiver {
    @Autowired
    private PublicSecurityService publicSecurityService;

    /**
     * 监听指定的消息中心队列 产生的消息
     * @param message  推送来的消息
     * @throws JMSException
     */
    @JmsListener(destination = MQDestination.PUBLIC_OPERATION_DESTINATION,containerFactory = "queueListenerFactory")
    public void receiveAppOperationMessage(Message message) throws JMSException {
        if (message instanceof ObjectMessage) {
            ObjectMessage objectMessage = (ObjectMessage) message;
            AppOperationMessage appOperationMessage = (AppOperationMessage) objectMessage.getObject();;
            try {
                publicSecurityService.createFileForSend(appOperationMessage);
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    }
    /**
     * 监听 文件监听器读取的文件内容发送到 的消息中心
     * @param message
     * @throws JMSException
     */
    @JmsListener(destination = MQDestination.PUBLIC_FILE_CONTENT_DESTINATION,containerFactory = "queueListenerFactory")
    public void receiveFileContentMessage(Message message) throws JMSException {
        if(message instanceof MapMessage){
            MapMessage mapMessage = (MapMessage) message;
            String fileContent = mapMessage.getString("fileContent");
            String fileName = mapMessage.getString("fileName");
            publicSecurityService.dealFile(fileContent,fileName);
        }
    }

}
